package com.yearner.reviewplan.common.util;

import com.yearner.reviewplan.common.exception.BaseAppException;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Objects;
import java.util.ResourceBundle;

/**
 * JedisUtil
 *
 * @author Pyg
 */
@Repository
public class JedisUtil {
    private static JedisPool sJedisPool;

    public JedisUtil(Environment environment) {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(1024);
        config.setMaxIdle(Integer.parseInt(Objects.requireNonNull(environment.getProperty("spring.redis.jedis.pool.max-idle"))));
        config.setMaxWaitMillis(Integer.parseInt(Objects.requireNonNull(environment.getProperty("spring.redis.jedis.pool.max-wait"))));
//        config.setTestOnBorrow(Boolean.parseBoolean(bundle.getString("redis.pool.testOnBorrow")));
//        config.setTestOnReturn(Boolean.parseBoolean(bundle.getString("redis.pool.testOnReturn")));
        sJedisPool = new JedisPool(config, environment.getProperty("spring.redis.host"), Integer.parseInt(Objects.requireNonNull(environment.getProperty("spring.redis.port"))));

    }

//    static {
//        ResourceBundle bundle = ResourceBundle.getBundle("jedisPool");
//        if (bundle == null) {
//            throw new IllegalArgumentException("[jedisPool.properties] is not found!");
//        }
//        JedisPoolConfig config = new JedisPoolConfig();
//        config.setMaxTotal(Integer.parseInt(bundle.getString("redis.pool.maxTotal")));
//        config.setMaxIdle(Integer.parseInt(bundle.getString("redis.pool.maxIdle")));
//        config.setMaxWaitMillis(Integer.parseInt(bundle.getString("redis.pool.maxWait")));
//        config.setTestOnBorrow(Boolean.parseBoolean(bundle.getString("redis.pool.testOnBorrow")));
//        config.setTestOnReturn(Boolean.parseBoolean(bundle.getString("redis.pool.testOnReturn")));
//        sJedisPool = new JedisPool(config, bundle.getString("redis.ip"), Integer.parseInt(bundle.getString("redis.port")));
//    }

    /**
     * 向某个 Map 添加键值对
     *
     * @param mapKey   Map的名称，某个集合，不存在则自动创建
     * @param valueKey key
     * @param value    值
     */
    public void hset(String mapKey, String valueKey, String value) {
        try (Jedis jedis = sJedisPool.getResource()) {
            jedis.hset(mapKey, valueKey, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从某个 Map 取值
     *
     * @param mapKey   Map的名称，某个集合
     * @param valueKey key
     * @return 若存在，则返回对应的值， 若不存在，则返回null
     */
    public String hget(String mapKey, String valueKey) {
        String value = null;
        try (Jedis jedis = sJedisPool.getResource()) {
            value = jedis.hget(mapKey, valueKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return value;
    }

    /**
     * 从某个 Map 删除元素
     *
     * @param mapKey   Map的名称，某个集合
     * @param valueKey key
     * @return 影响数量
     */
    public Long hdel(String mapKey, String... valueKey) {
        Long value = null;
        try (Jedis jedis = sJedisPool.getResource()) {
            value = jedis.hdel(mapKey, valueKey);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return value;
    }

    /**
     * 设置过期时间
     *
     * @param key
     * @param second
     * @return
     */
    public Long expire(String key, int second) {
        Long value = null;
        try (Jedis jedis = sJedisPool.getResource()) {
            value = jedis.expire(key, second);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return value;
    }

    public String set(String key, String value) {
        String returnValue = null;
        try (Jedis jedis = sJedisPool.getResource()) {
            returnValue = jedis.set(key, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return returnValue;
    }

    public String get(String key) {
        String returnValue = null;
        try (Jedis jedis = sJedisPool.getResource()) {
            returnValue = jedis.get(key);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return returnValue;
    }

    /**
     * 删除元素
     *
     * @param key key
     * @return 影响数量
     */
    public Long del(String... key) {
        Long value = null;
        try (Jedis jedis = sJedisPool.getResource()) {
            value = jedis.del(key);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return value;
    }


}
